package cn.kgm.user.server.abs;

import cn.kgm.core.ulid.Ulid;
import cn.kgm.core.ulid.UlidCreator;
import cn.kgm.user.dto.constant.UserConstant;
import cn.kgm.user.dto.dto.UserRegisterDTO;
import cn.kgm.user.dto.entity.User;
import cn.kgm.user.dto.entity.UserLoginType;
import cn.kgm.user.server.mapper.UserMapper;
import cn.kgm.user.server.service.UserLoginTypeService;
import cn.kgm.user.server.service.UserService;
import cn.kgm.user.server.struct.UserMappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * #技术改变自己
 *
 * @author: kappi
 * @email: yolo.jk@qq.com
 * @date: 2024/4/9 22:52周二
 * @package: cn.kgm.user.server.abs.UserAbs
 */
@Slf4j
@Component(value = UserConstant.IocName.USER_ABS)
public abstract class UserAbs extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserLoginTypeService loginTypeService;

    /**
     * 用户注册
     * @param dto 注册参数
     * @return 用户信息
     */
    @Override
    public User register(UserRegisterDTO dto) {
        Ulid ulid = UlidCreator.getUlid();
        // 获取可排序uuid,减少mysql索引空隙,优化查询
        dto.setUuid(ulid.toLowerCase());
        User user = UserMappers.INSTANCE.mapToEntity(dto);
        this.save(user);

        loginTypeService.save(UserLoginType.builder()
                .userId(user.getId())
                .type(dto.getLoginType())
                .build());
        return user;
    }
}
